package com.yoooya.base.admin.interceptor;

import com.yoooya.base.admin.utils.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @创建人 Sun Yijing
 * @创建时间 2018-06-28
 */
public class MonitorInterceptor extends HandlerInterceptorAdapter {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String uri = request.getServletPath();
        logger.debug("收到来自[{}]的命令[{}]请求.", WebUtils.getClientIp(request), uri);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
        String uri = request.getServletPath();
        if(null == e) {
            logger.debug("应答来自[{}]的命令[{}]请求.", WebUtils.getClientIp(request), uri);
        } else {
            logger.error("来自[{}]的命令[{}]请求处理有误[params={}].", WebUtils.getClientIp(request), uri, request.getParameterMap(), e);
        }
    }
    
}
